//
//  WishlistViewController.m
//  iShop
//
//  Created by svp on 20/12/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "WishlistViewController.h"


@implementation WishlistViewController

@synthesize tableView = _tableView;
@synthesize changeModeButton = _changeModeButton;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


-(void) viewDidLoad
{
	[super viewDidLoad];
	
	count = 2;
	//_tableView.editing = YES;
}

- (void)dealloc {
	NSLog(@"WishlistViewController dealloc");
	[_tableView release];
    [super dealloc];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEditing]){
		return YES;
    }
	
    return NO;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return count + [tableView isEditing];
}

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if ([tableView isEditing] && indexPath.row != count) {
		return UITableViewCellEditingStyleDelete;
	}
	return UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{
	
    if (editingStyle == UITableViewCellEditingStyleDelete)
		
    {		
        
		count --;
    } else if (editingStyle == UITableViewCellEditingStyleInsert)
		
    {
		
        count ++;
		
    }
	
	[tableView reloadData];
	
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShopCell"];
	if (!cell) {
		NSLog(@"Create cell");
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ShopCell"] autorelease];
	}
	
	if (![tableView isEditing] || ([tableView isEditing] && indexPath.row != count)) {
		cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ico_ip_chat" ofType:@"png"]];
		cell.imageView.backgroundColor = [UIColor redColor];
		cell.textLabel.text = @"San pham";
		cell.detailTextLabel.text = @"So Luong";
		UIImageView *temp = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ico_ip_chat" ofType:@"png"]]];
		temp.backgroundColor = [UIColor yellowColor];
		cell.accessoryView = temp;
		[temp release];
	}
	else if([tableView isEditing]){
		cell.textLabel.text = @"More San pham";
	}
	
	return cell;
}

-(IBAction) savePress:(id) sender;
{
	NSLog(@"savePress");
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}

-(IBAction) cancelPress:(id) sender;
{
	NSLog(@"cancelPress");
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}

-(IBAction) changeMode:(id) sender
{
	BOOL canEdit = [_tableView isEditing];
	canEdit = !canEdit;
	_tableView.editing = canEdit;
	_changeModeButton.style =  canEdit ? UIBarButtonItemStyleDone : UIBarButtonItemStyleBordered;
	[_tableView reloadData];
}

@end
